package com.perye.dokit.weController;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.entity.User;
import com.perye.dokit.exception.BadRequestException;
import com.perye.dokit.service.WeWeddingStyleService;
import com.perye.dokit.service.WeddingTypeService;
import com.perye.dokit.utils.*;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.util.List;

/**
 * @ClassName TestController
 * @Date 2022/3/11 16:55
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/weddingType")
public class WeddingTypeController extends BaseController {

    @Autowired
    private WeddingTypeService weddingTypeService;

    @Autowired
    private WeWeddingStyleService weWeddingStyleService;

    @Value("${uploadFilePath}")
    String uploadFilePath;

    @Value("${uploadFileMapping}")
    String uploadFileMapping;

    /**
     * @Author: zhongdi
     * @Date: 2021/6/7 15:17
     * @Description: 查询智能硬件列表
     */
    @Log("查询婚礼类型列表")
    @ApiOperation("查询婚礼类型列表")
    @GetMapping()
    @PreAuthorize("@dokit.check('weddingType:list')")
    public ResponseEntity<Object> weddingTypeList() {
        PageData pageDataList = null;
        try {
            PageData pageData = this.getPageData();
            int page = Integer.parseInt(pageData.getString("page")) + 1;
            PageHelper.startPage(page, Integer.parseInt(pageData.getString("size")));

            List<PageData> deptDtos = weddingTypeService.queryWeddingTypeList(pageData);


            PageInfo<PageData> pageInfo = new PageInfo<PageData>(deptDtos);

            int count = (int) pageInfo.getTotal();
            pageDataList = ArrayAssembly.pageList(deptDtos, count);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return new ResponseEntity<>(pageDataList, HttpStatus.OK);
    }

    @Log("删除婚礼类型")
    @ApiOperation("删除婚礼类型")
    @PreAuthorize("@dokit.check('weddingType:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) throws Exception {

        weddingTypeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


/*    @Log("hardware查询字典信息")
    @ApiOperation("hardware查询字典信息")
    @RequestMapping("/queryDictList")
    @ResponseBody
    public ResponseEntity<Object> queryDictList(@RequestBody PageData pageData) {
        List<PageData> dictList = slHardwareService.queryDictList(pageData);
        return new ResponseEntity<>(dictList, HttpStatus.OK);
    }*/

    @Log("新增婚礼类型")
    @ApiOperation("新增婚礼类型")
    @PostMapping
    @PreAuthorize("@dokit.check('weddingType:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PageData pageData) throws Exception {

        int num = weddingTypeService.create(pageData);
        if (num <= 0) {
            throw new BadRequestException("新增失败");
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改婚礼类型")
    @ApiOperation("修改婚礼类型")
    @PutMapping
    @PreAuthorize("@dokit.check('weddingType:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PageData pageData) throws Exception {

        weddingTypeService.update(pageData);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping("/updateIsOpenState")
    @ResponseBody
    public PageData updateIsOpenState(@Validated @RequestBody PageData pageData) {
        PageData result = new PageData();
        try {
            weddingTypeService.update(pageData);
            result.put("code", "00");
            result.put("msg", "修改成功");
            result.put("data", null);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "01");
            result.put("msg", "修改失败");
            result.put("data", null);
        }
        return result;
    }


    @RequestMapping("/uploadFile")
    @ResponseBody
    public PageData uploadFile(@RequestParam("file") MultipartFile file) {
        //操作MultipartFile uploadImage, String moduleName
        //返回结果
        PageData result = new PageData();
        //参数  uploadFilePath 上传文件路径//uploadFileMapping 映射路径

        try {
            String filePath = UploadFile.uploadFile(file, uploadFilePath, uploadFileMapping);
            PageData pd = new PageData();
            String id = IdUtil.simpleUUID();//图片id
            pd.put("id", id);
            pd.put("relationId", "");
            pd.put("type", 0);//'类型：0：图片；1：视频',
            pd.put("url", filePath);
            pd.put("sort", 0);
            pd.put("isDelete", 0);
            pd.put("createTime", DateUtils.getTime());
            pd.put("updateTime", DateUtils.getTime());
            weWeddingStyleService.insterImg(pd);
            result.put("code", "00");
            result.put("filePath", filePath);
            result.put("filePathId", id);
            result.put("msg", "上传成功");
        } catch (Exception e) {

        }
        return result;
    }

}
